package com.hongyi.modelcontest.core.util;

import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.*;
import com.qcloud.cos.region.Region;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

public class COSUtils {
    private static final Logger logger = LoggerFactory.getLogger(COSUtils.class);

    private static final String PHOTO = "childphoto-1258443025";
    private static final String DEPT_LOGO = "childlogo-1258443025";

    public static String uploadPhoto(String key, MultipartFile picture) {
        return uploadFile(PHOTO, key, picture);
    }

    public static String uploadLogo(String key, MultipartFile logo) {
        return uploadFile(DEPT_LOGO, key, logo);
    }

    public static String uploadFile(String photo, String key, MultipartFile picture) {
        // 指定要上传到的存储桶
        ObjectMetadata objectMetadata = new ObjectMetadata();
        // 设置输入流长度为500
        objectMetadata.setContentLength(picture.getSize());
        // 设置 Content type, 默认是 application/octet-stream
        objectMetadata.setContentType(picture.getContentType());
        try {
            String filenameExtension = StringUtils.getFilenameExtension(picture.getOriginalFilename());
            PutObjectRequest putObjectRequest = new PutObjectRequest(photo, key + "." + filenameExtension, picture
                    .getInputStream(),
                    objectMetadata);
            COSClient cosClient = getCosClient();
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            //logger.info("文件上传结果:{}", JSONObject.toJSONString(putObjectResult));
            cosClient.shutdown();
            return key + "." + filenameExtension;
        } catch (IOException e) {
            logger.error("文件上传失败:{}", e);
            throw new ServiceException(500, e.getMessage());
        }
    }

    public static COSObject dowloadPhoto(String key) {
        return dowload(PHOTO, key);
    }

    public static COSObject dowloadLogo(String key) {
        return dowload(DEPT_LOGO, key);
    }

    public static COSObject dowload(String photo, String key) {
        GetObjectRequest getObjectRequest = new GetObjectRequest(photo, key);
        COSClient cosClient = getCosClient();
        COSObject object = cosClient.getObject(getObjectRequest);
        cosClient.shutdown();
        return object;
    }


    public static COSClient getCosClient() {
        // 1 初始化用户身份信息（secretId, secretKey）。
        COSCredentials cred = new BasicCOSCredentials("AKIDygdqqbOLZBKGSPlj5de5KHyFGtK4dOjp", "KRIjIpxRrxAYhePthPNq1wekdcZFm156");
        // 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        // clientConfig中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者接口文档 FAQ 中说明。
        ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"));
        // 3 生成 cos 客户端。
        return new COSClient(cred, clientConfig);
    }
}
